import LogFactory from "log/LogFactory";

import {SecurityMenu} from "type/SecurityMenu";
import {createAsyncThunk, createSlice} from "@reduxjs/toolkit";
import {requestDelete, requestGet, requestPost, requestPut} from "util/request";
import {Page, PagingData} from "type/Page";
import System from "global/System";
import {StateBaseType} from "../StateBaseType";
import {ReduxStatus} from "enum/ReduxStatus";

const log = LogFactory.getLog("menuReducer") ;

export type MenuState = StateBaseType<SecurityMenu> ;

const initialState: MenuState = {
    data: [] ,
    pageSize: System.getInstance().getUserConfig().menuPageSize,
    current: 1,
    total: 0,
    loading: false,
    status: ReduxStatus.INIT,
} ;
/**
 * 异步获取菜单分页数据
 */
export const fetchPageData = createAsyncThunk(
    'menu/fetchPageData',
    async (paging: PagingData) => {
        log.request("异步获取菜单分页数据", paging) ;
        const response = await requestGet<Page<SecurityMenu>>(`/manager/security-menu/page/${paging.current}/${paging.pageSize}`) ;
        return response.data ;
    }
) ;
/**
 * 异步添加菜单
 */
export const fetchAddData = createAsyncThunk(
    'menu/fetchAddData',
    async (data: SecurityMenu) => {
        const response = await requestPut<number>("/manager/security-menu",data) ;
        return response.data ;
    }
) ;
export const fetchGetData = createAsyncThunk(
    'menu/fetchGetData',
    async (menuId: number) => {
        const response = await requestGet<SecurityMenu>("/manager/security-menu/" + menuId) ;
        return response.data ;
    }
) ;

/**
 * 异步修改菜单
 */
export const fetchUpdateData = createAsyncThunk(
    'menu/fetchUpdateData',
    async (data: SecurityMenu) => {
        const response = await requestPost<number>("/manager/security-menu",data) ;
        return response.data ;
    }
) ;

export const fetchDeleteData = createAsyncThunk(
    'menu/fetchDeleteData',
    async (menuId: number) => {
        const response = await requestDelete<number>("/manager/security-menu/" + menuId) ;
        return response.data ;
    }
) ;

export const menuReducer = createSlice({
    name: 'menu',
    initialState,
    reducers: {

    },
    extraReducers: (builder) => {
        builder.addCase(fetchPageData.pending, (state) => {
            state.loading = true ;
            state.status = ReduxStatus.PENDING ;
        }) ;
        builder.addCase(fetchPageData.fulfilled, (state, action) => {
            state.data = action.payload.data ;
            state.pageSize = action.payload.pageSize ;
            state.current = action.payload.currentPage ;
            state.total = action.payload.total ;
            state.loading = false ;
            state.status = ReduxStatus.SUCCESS ;
        }) ;

        builder.addCase(fetchAddData.fulfilled, (state) => {
            state.status = ReduxStatus.REFRESH ;
            log.debug("添加菜单数据成功,请求刷新页面") ;
        }) ;

        builder.addCase(fetchUpdateData.fulfilled, (state) => {
            state.status = ReduxStatus.REFRESH ;
            log.debug("修改菜单数据成功,请求刷新页面") ;
        });

        // builder.addCase(fetchGetData.pending, (state) => {
        //     log.debug("根据ID请求菜单数据,执行中") ;
        // });
        builder.addCase(fetchGetData.fulfilled, (state) => {
            log.debug("根据ID请求菜单数据,执行成功") ;
        });

        builder.addCase(fetchDeleteData.fulfilled, (state,payload) => {
            state.status = ReduxStatus.REFRESH ;
            log.debug("删除菜单数据成功,请求刷新页面", payload) ;
        });
    }

});

// export const {} = menuReducer.actions ;
export default menuReducer.reducer ;